function YoutubeView() {
	var self = Ti.UI.createView();

	var toolActInd = Titanium.UI.createActivityIndicator();
	
	self.add(toolActInd);

	var tableview;
	var data = [];
	var webModal;
	var webModalView;
	var currentLink;
	var xhr = Ti.Network.createHTTPClient();

	self.playYouTube = function(vtitle, vguid) {
		var ytVideoSrc = "http://www.youtube.com/v/" + vguid;
		var thumbPlayer = '<html><head><style type="text/css"> body { background-color: black;color: white;} </style></head><body style="margin:0"><br/><br/><center><embed id="yt" src="' + ytVideoSrc + '" type="application/x-shockwave-flash" width="100%" height="75%"></embed></center></body></html>';
		self.showHTMLContent(vtitle, 'http://www.youtube.com/watch?v=' + vguid, thumbPlayer);
	}

	self.showHTMLContent = function(wTitle, wUrl, wHTMLContent) {
		//store the link for later use
		currentLink = wUrl;
		//create the window to hold the web view
		webModal = Ti.UI.createWindow({});
		//set the orientation modes for basically any which way
		webModal.orientationModes = [Titanium.UI.PORTRAIT, Titanium.UI.LANDSCAPE_LEFT, Titanium.UI.LANDSCAPE_RIGHT];
		//create the webview aka the embedded web browser (webkit/safari)
		webModalView = Ti.UI.createWebView();
		webModalView.scalesPageToFit = true;
		//add the web video to the modal window
		webModal.add(webModalView);
		//set the title of the window
		webModal.title = wTitle;
		//if you are using a tab UI in the app, this will open the window
		self.parent.open(webModal, {
			animated : true
		});
		//set the HTML to display to the markup passed into the function
		webModalView.html = wHTMLContent;
	};

	self.doYouTubeSearch = function(channel, searchTerm) {
		//first show a “loading” spinning indicator to the user
		toolActInd.message = 'Loading videos...';
		// win.setToolbar([toolActInd], {
			// animated : true
		// });
		toolActInd.show();
		//create the YouTube API search URL from the function parameters
		var searchUrl = 'http://gdata.youtube.com/feeds/api/videos?alt=rss&q=' + escape(searchTerm) + "&orderby=published&max-results=10&v=2";
		//use the xhr http client object to do an HTTP GET request to the URL
		xhr.open("GET", searchUrl);		
		xhr.send();
	}

	xhr.onload = function() {
		try {
			//the doc object holds the response structure
			var doc;
			//check whether the data coming back is in XML format or not
			if (!this.responseXML) {
				//if not XML you have to convert it to XML
				doc = Titanium.XML.parseString(this.responseText).documentElement;
			} else {
				//if it is XML, then just set the doc variable
				doc = this.responseXML.documentElement;
			}
			
			//now we can easily get a list of items from teh results
			var items = doc.getElementsByTagName("item");
			//some simple variables for tracking the loop
			var x = 0;
			var c;
			//now just loop through the response array to see what videos we have
			for ( c = 0; c < items.length; c++) {
				//get the current item
				var item = items.item(c);
				//get the text for the video title tag using standard DOM XML calls
				var title = item.getElementsByTagName("title").item(0).text;
				//build up a summary string to display below the title
				var summary = "";
				if (item.getElementsByTagName("pubDate")) {
					summary = item.getElementsByTagName("pubDate").item(0).text;
				}
				//get the link to the youtube video
				var link = "";
				if (item.getElementsByTagName("link")) {
					link = item.getElementsByTagName("link").item(0).text;
				}
				//now here is where we perform a trick
				//we find the GUID code from within the link b/c we know the link format
				var guid = link.substring(link.indexOf("?v=") + 3);
				guid = guid.substring(0, guid.indexOf("&"));
				//now we can use that guid to load up a thumbnail image
				var thumbnail = "http://i.ytimg.com/vi/" + guid + "/2.jpg";
				//okay we have all the data we need for that item
				//now we need to create a row to add to the table in order to display it
				//create the row item and set the height to 80 pixels
				var row = Ti.UI.createTableViewRow({
					height : 80
				});
				//set parameters for the row so we can get the youtube data out later
				row.url = link;
				row.guid = guid;
				row.videotitle = title;
				//create a label for displaying the title and add it to the row
				var labelTitle = Ti.UI.createLabel({
					text : title,
					left : 105,
					top : 10,
					height : 40,
					font : {
						fontSize : 16
					}
				});
				row.add(labelTitle);
				//create a label for the summary and add it to the row
				var labelSummary = Ti.UI.createLabel({
					text : summary,
					left : 105,
					top : 45,
					font : {
						fontSize : 12
					}
				});
				row.add(labelSummary);
				//create an image from the thumbnail, and add it to the row
				// var img = Ti.UI.createImageView({
					// url : thumbnail,
					// left : 0,
					// height : 80,
					// width : 100
				// });
				// row.add(img);
				//add the row to the data array
				data[x++] = row;
			}
			//if tableview has been created, reset the data on the table
			//you can update data on the table multiple times
			if (tableview) {
				tableview.setData([]);
				tableview.setData(data);
			} else {
				//if table has not been created, build it up with the data array
				tableview = Titanium.UI.createTableView({
					data : data
				});
				//add the table to the current window for display
				self.add(tableview);
				//add a ‘click’ listener so that when someone taps on a row
				//the video will be played using the function we defined earlier
				tableview.addEventListener('click', function(e) {
					self.playYouTube(e.row.videotitle, e.row.guid);
				});
			}
		} catch(E) {
			//if anything bad happens, show the error to the user and log it
			Titanium.API.debug(E);
			Titanium.UI.createAlertDialog({
				title : 'Youtube Search',
				message : 'No videos were found for this search'
			}).show();
		}
		//hide the spinning ‘loading’ widget
		toolActInd.hide();
		// win.setToolbar(null, {
			// animated : true
		// });
	};

	return self;
};

module.exports = YoutubeView;
